<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>ib_insync.client &mdash; ib_insync 0.9.70 documentation</title>
      <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
      <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
    <link rel="canonical" href="https://ib_insync.readthedocs.io_modules/ib_insync/client.html"/>
  
        <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/doctools.js"></script>
    <script src="../../_static/js/theme.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
            <a href="../../index.html" class="icon icon-home"> ib_insync
          </a>
              <div class="version">
                0.9
              </div>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../readme.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../api.html">API docs</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../notebooks.html">Notebooks</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../recipes.html">Code recipes</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../code.html">Source code</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../changelog.html">Changelog</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../links.html">Links</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">ib_insync</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../../index.html" class="icon icon-home"></a> &raquo;</li>
          <li><a href="../index.html">Module code</a> &raquo;</li>
      <li>ib_insync.client</li>
      <li class="wy-breadcrumbs-aside">
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <h1>Source code for ib_insync.client</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;Socket client for communicating with Interactive Brokers.&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">asyncio</span>
<span class="kn">import</span> <span class="nn">io</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">struct</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">deque</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">List</span><span class="p">,</span> <span class="n">Optional</span>

<span class="kn">from</span> <span class="nn">eventkit</span> <span class="kn">import</span> <span class="n">Event</span>

<span class="kn">from</span> <span class="nn">.connection</span> <span class="kn">import</span> <span class="n">Connection</span>
<span class="kn">from</span> <span class="nn">.contract</span> <span class="kn">import</span> <span class="n">Contract</span>
<span class="kn">from</span> <span class="nn">.decoder</span> <span class="kn">import</span> <span class="n">Decoder</span>
<span class="kn">from</span> <span class="nn">.objects</span> <span class="kn">import</span> <span class="n">ConnectionStats</span>
<span class="kn">from</span> <span class="nn">.util</span> <span class="kn">import</span> <span class="n">UNSET_DOUBLE</span><span class="p">,</span> <span class="n">UNSET_INTEGER</span><span class="p">,</span> <span class="n">dataclassAsTuple</span><span class="p">,</span> <span class="n">run</span>


<div class="viewcode-block" id="Client"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client">[docs]</a><span class="k">class</span> <span class="nc">Client</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Replacement for ``ibapi.client.EClient`` that uses asyncio.</span>

<span class="sd">    The client is fully asynchronous and has its own</span>
<span class="sd">    event-driven networking code that replaces the</span>
<span class="sd">    networking code of the standard EClient.</span>
<span class="sd">    It also replaces the infinite loop of ``EClient.run()``</span>
<span class="sd">    with the asyncio event loop. It can be used as a drop-in</span>
<span class="sd">    replacement for the standard EClient as provided by IBAPI.</span>

<span class="sd">    Compared to the standard EClient this client has the following</span>
<span class="sd">    additional features:</span>

<span class="sd">    * ``client.connect()`` will block until the client is ready to</span>
<span class="sd">      serve requests; It is not necessary to wait for ``nextValidId``</span>
<span class="sd">      to start requests as the client has already done that.</span>
<span class="sd">      The reqId is directly available with :py:meth:`.getReqId()`.</span>

<span class="sd">    * ``client.connectAsync()`` is a coroutine for connecting asynchronously.</span>

<span class="sd">    * When blocking, ``client.connect()`` can be made to time out with</span>
<span class="sd">      the timeout parameter (default 2 seconds).</span>

<span class="sd">    * Optional ``wrapper.priceSizeTick(reqId, tickType, price, size)`` that</span>
<span class="sd">      combines price and size instead of the two wrapper methods</span>
<span class="sd">      priceTick and sizeTick.</span>

<span class="sd">    * Automatic request throttling.</span>

<span class="sd">    * Optional ``wrapper.tcpDataArrived()`` method;</span>
<span class="sd">      If the wrapper has this method it is invoked directly after</span>
<span class="sd">      a network packet has arrived.</span>
<span class="sd">      A possible use is to timestamp all data in the packet with</span>
<span class="sd">      the exact same time.</span>

<span class="sd">    * Optional ``wrapper.tcpDataProcessed()`` method;</span>
<span class="sd">      If the wrapper has this method it is invoked after the</span>
<span class="sd">      network packet&#39;s data has been handled.</span>
<span class="sd">      A possible use is to write or evaluate the newly arrived data in</span>
<span class="sd">      one batch instead of item by item.</span>

<span class="sd">    Attributes:</span>
<span class="sd">      MaxRequests (int):</span>
<span class="sd">        Throttle the number of requests to ``MaxRequests`` per</span>
<span class="sd">        ``RequestsInterval`` seconds. Set to 0 to disable throttling.</span>
<span class="sd">      RequestsInterval (float):</span>
<span class="sd">        Time interval (in seconds) for request throttling.</span>
<span class="sd">      MinClientVersion (int):</span>
<span class="sd">        Client protocol version.</span>
<span class="sd">      MaxClientVersion (int):</span>
<span class="sd">        Client protocol version.</span>

<span class="sd">    Events:</span>
<span class="sd">      * ``apiStart`` ()</span>
<span class="sd">      * ``apiEnd`` ()</span>
<span class="sd">      * ``apiError`` (errorMsg: str)</span>
<span class="sd">      * ``throttleStart`` ()</span>
<span class="sd">      * ``throttleEnd`` ()</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">events</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;apiStart&#39;</span><span class="p">,</span> <span class="s1">&#39;apiEnd&#39;</span><span class="p">,</span> <span class="s1">&#39;apiError&#39;</span><span class="p">,</span> <span class="s1">&#39;throttleStart&#39;</span><span class="p">,</span> <span class="s1">&#39;throttleEnd&#39;</span><span class="p">)</span>

    <span class="n">MaxRequests</span> <span class="o">=</span> <span class="mi">45</span>
    <span class="n">RequestsInterval</span> <span class="o">=</span> <span class="mi">1</span>

    <span class="n">MinClientVersion</span> <span class="o">=</span> <span class="mi">142</span>
    <span class="n">MaxClientVersion</span> <span class="o">=</span> <span class="mi">163</span>

    <span class="p">(</span><span class="n">DISCONNECTED</span><span class="p">,</span> <span class="n">CONNECTING</span><span class="p">,</span> <span class="n">CONNECTED</span><span class="p">)</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">wrapper</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span> <span class="o">=</span> <span class="n">wrapper</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">decoder</span> <span class="o">=</span> <span class="n">Decoder</span><span class="p">(</span><span class="n">wrapper</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">apiStart</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;apiStart&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">apiEnd</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;apiEnd&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">apiError</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;apiError&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">throttleStart</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;throttleStart&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">throttleEnd</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;throttleEnd&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s1">&#39;ib_insync.client&#39;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">conn</span> <span class="o">=</span> <span class="n">Connection</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">conn</span><span class="o">.</span><span class="n">hasData</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_onSocketHasData</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">conn</span><span class="o">.</span><span class="n">disconnected</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_onSocketDisconnected</span>

        <span class="c1"># extra optional wrapper methods</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_priceSizeTick</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">wrapper</span><span class="p">,</span> <span class="s1">&#39;priceSizeTick&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_tcpDataArrived</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">wrapper</span><span class="p">,</span> <span class="s1">&#39;tcpDataArrived&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_tcpDataProcessed</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">wrapper</span><span class="p">,</span> <span class="s1">&#39;tcpDataProcessed&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">host</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">port</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">clientId</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">optCapab</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">connectOptions</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span>

<div class="viewcode-block" id="Client.reset"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reset">[docs]</a>    <span class="k">def</span> <span class="nf">reset</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">connState</span> <span class="o">=</span> <span class="n">Client</span><span class="o">.</span><span class="n">DISCONNECTED</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_apiReady</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_serverVersion</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_data</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_hasReqId</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_reqIdSeq</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_accounts</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_startTime</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_numBytesRecv</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_numMsgRecv</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_isThrottling</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_msgQ</span> <span class="o">=</span> <span class="n">deque</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_timeQ</span> <span class="o">=</span> <span class="n">deque</span><span class="p">()</span></div>

<div class="viewcode-block" id="Client.serverVersion"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.serverVersion">[docs]</a>    <span class="k">def</span> <span class="nf">serverVersion</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_serverVersion</span></div>

<div class="viewcode-block" id="Client.run"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.run">[docs]</a>    <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
        <span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span></div>

<div class="viewcode-block" id="Client.isConnected"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.isConnected">[docs]</a>    <span class="k">def</span> <span class="nf">isConnected</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">connState</span> <span class="o">==</span> <span class="n">Client</span><span class="o">.</span><span class="n">CONNECTED</span></div>

<div class="viewcode-block" id="Client.isReady"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.isReady">[docs]</a>    <span class="k">def</span> <span class="nf">isReady</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">bool</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Is the API connection up and running?&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_apiReady</span></div>

<div class="viewcode-block" id="Client.connectionStats"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.connectionStats">[docs]</a>    <span class="k">def</span> <span class="nf">connectionStats</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">ConnectionStats</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Get statistics about the connection.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">isReady</span><span class="p">():</span>
            <span class="k">raise</span> <span class="ne">ConnectionError</span><span class="p">(</span><span class="s1">&#39;Not connected&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">ConnectionStats</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_startTime</span><span class="p">,</span>
            <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">_startTime</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_numBytesRecv</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">conn</span><span class="o">.</span><span class="n">numBytesSent</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_numMsgRecv</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">conn</span><span class="o">.</span><span class="n">numMsgSent</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.getReqId"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.getReqId">[docs]</a>    <span class="k">def</span> <span class="nf">getReqId</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Get new request ID.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">isReady</span><span class="p">():</span>
            <span class="k">raise</span> <span class="ne">ConnectionError</span><span class="p">(</span><span class="s1">&#39;Not connected&#39;</span><span class="p">)</span>
        <span class="n">newId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_reqIdSeq</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_reqIdSeq</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">return</span> <span class="n">newId</span></div>

<div class="viewcode-block" id="Client.updateReqId"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.updateReqId">[docs]</a>    <span class="k">def</span> <span class="nf">updateReqId</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">minReqId</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Update the next reqId to be at least ``minReqId``.&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_reqIdSeq</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_reqIdSeq</span><span class="p">,</span> <span class="n">minReqId</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.getAccounts"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.getAccounts">[docs]</a>    <span class="k">def</span> <span class="nf">getAccounts</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="nb">str</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;Get the list of account names that are under management.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">isReady</span><span class="p">():</span>
            <span class="k">raise</span> <span class="ne">ConnectionError</span><span class="p">(</span><span class="s1">&#39;Not connected&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_accounts</span></div>

<div class="viewcode-block" id="Client.setConnectOptions"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.setConnectOptions">[docs]</a>    <span class="k">def</span> <span class="nf">setConnectOptions</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connectOptions</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Set additional connect options.</span>

<span class="sd">        Args:</span>
<span class="sd">            connectOptions: Use &quot;+PACEAPI&quot; to use request-pacing built</span>
<span class="sd">                into TWS/gateway 974+.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">connectOptions</span> <span class="o">=</span> <span class="n">connectOptions</span><span class="o">.</span><span class="n">encode</span><span class="p">()</span></div>

<div class="viewcode-block" id="Client.connect"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.connect">[docs]</a>    <span class="k">def</span> <span class="nf">connect</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">host</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">port</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">clientId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
            <span class="n">timeout</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">float</span><span class="p">]</span> <span class="o">=</span> <span class="mf">2.0</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Connect to a running TWS or IB gateway application.</span>

<span class="sd">        Args:</span>
<span class="sd">            host: Host name or IP address.</span>
<span class="sd">            port: Port number.</span>
<span class="sd">            clientId: ID number to use for this client; must be unique per</span>
<span class="sd">                connection.</span>
<span class="sd">            timeout: If establishing the connection takes longer than</span>
<span class="sd">                ``timeout`` seconds then the ``asyncio.TimeoutError`` exception</span>
<span class="sd">                is raised. Set to 0 to disable timeout.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connectAsync</span><span class="p">(</span><span class="n">host</span><span class="p">,</span> <span class="n">port</span><span class="p">,</span> <span class="n">clientId</span><span class="p">,</span> <span class="n">timeout</span><span class="p">))</span></div>

<div class="viewcode-block" id="Client.connectAsync"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.connectAsync">[docs]</a>    <span class="k">async</span> <span class="k">def</span> <span class="nf">connectAsync</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">host</span><span class="p">,</span> <span class="n">port</span><span class="p">,</span> <span class="n">clientId</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mf">2.0</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
                <span class="sa">f</span><span class="s1">&#39;Connecting to </span><span class="si">{</span><span class="n">host</span><span class="si">}</span><span class="s1">:</span><span class="si">{</span><span class="n">port</span><span class="si">}</span><span class="s1"> with clientId </span><span class="si">{</span><span class="n">clientId</span><span class="si">}</span><span class="s1">...&#39;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">host</span> <span class="o">=</span> <span class="n">host</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">port</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">port</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">clientId</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">clientId</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">connState</span> <span class="o">=</span> <span class="n">Client</span><span class="o">.</span><span class="n">CONNECTING</span>
            <span class="n">timeout</span> <span class="o">=</span> <span class="n">timeout</span> <span class="ow">or</span> <span class="kc">None</span>
            <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">wait_for</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">conn</span><span class="o">.</span><span class="n">connectAsync</span><span class="p">(</span><span class="n">host</span><span class="p">,</span> <span class="n">port</span><span class="p">),</span> <span class="n">timeout</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Connected&#39;</span><span class="p">)</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;API</span><span class="se">\0</span><span class="s1">&#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prefix</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;v</span><span class="si">%d</span><span class="s1">..</span><span class="si">%d%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">MinClientVersion</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">MaxClientVersion</span><span class="p">,</span>
                <span class="sa">b</span><span class="s1">&#39; &#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">connectOptions</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">connectOptions</span> <span class="k">else</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">conn</span><span class="o">.</span><span class="n">sendMsg</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
            <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">wait_for</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">apiStart</span><span class="p">,</span> <span class="n">timeout</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;API connection ready&#39;</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">BaseException</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">disconnect</span><span class="p">()</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="sa">f</span><span class="s1">&#39;API connection failed: </span><span class="si">{</span><span class="n">e</span><span class="si">!r}</span><span class="s1">&#39;</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">apiError</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="ne">ConnectionRefusedError</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s1">&#39;Make sure API port on TWS/IBG is open&#39;</span><span class="p">)</span>
            <span class="k">raise</span></div>

<div class="viewcode-block" id="Client.disconnect"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.disconnect">[docs]</a>    <span class="k">def</span> <span class="nf">disconnect</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Disconnect from IB connection.&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Disconnecting&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">connState</span> <span class="o">=</span> <span class="n">Client</span><span class="o">.</span><span class="n">DISCONNECTED</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">conn</span><span class="o">.</span><span class="n">disconnect</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span></div>

<div class="viewcode-block" id="Client.send"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.send">[docs]</a>    <span class="k">def</span> <span class="nf">send</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">fields</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Serialize and send the given fields using the IB socket protocol.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">isConnected</span><span class="p">():</span>
            <span class="k">raise</span> <span class="ne">ConnectionError</span><span class="p">(</span><span class="s1">&#39;Not connected&#39;</span><span class="p">)</span>

        <span class="n">msg</span> <span class="o">=</span> <span class="n">io</span><span class="o">.</span><span class="n">StringIO</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">:</span>
            <span class="n">typ</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="n">field</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">field</span> <span class="ow">in</span> <span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="n">UNSET_INTEGER</span><span class="p">,</span> <span class="n">UNSET_DOUBLE</span><span class="p">):</span>
                <span class="n">s</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
            <span class="k">elif</span> <span class="n">typ</span> <span class="ow">in</span> <span class="p">(</span><span class="nb">str</span><span class="p">,</span> <span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">):</span>
                <span class="n">s</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">field</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">typ</span> <span class="ow">is</span> <span class="nb">bool</span><span class="p">:</span>
                <span class="n">s</span> <span class="o">=</span> <span class="s1">&#39;1&#39;</span> <span class="k">if</span> <span class="n">field</span> <span class="k">else</span> <span class="s1">&#39;0&#39;</span>
            <span class="k">elif</span> <span class="n">typ</span> <span class="ow">is</span> <span class="nb">list</span><span class="p">:</span>
                <span class="c1"># list of TagValue</span>
                <span class="n">s</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="n">v</span><span class="o">.</span><span class="n">tag</span><span class="si">}</span><span class="s1">=</span><span class="si">{</span><span class="n">v</span><span class="o">.</span><span class="n">value</span><span class="si">}</span><span class="s1">;&#39;</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">field</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">Contract</span><span class="p">):</span>
                <span class="n">c</span> <span class="o">=</span> <span class="n">field</span>
                <span class="n">s</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\0</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="p">(</span>
                    <span class="n">c</span><span class="o">.</span><span class="n">conId</span><span class="p">,</span> <span class="n">c</span><span class="o">.</span><span class="n">symbol</span><span class="p">,</span> <span class="n">c</span><span class="o">.</span><span class="n">secType</span><span class="p">,</span>
                    <span class="n">c</span><span class="o">.</span><span class="n">lastTradeDateOrContractMonth</span><span class="p">,</span> <span class="n">c</span><span class="o">.</span><span class="n">strike</span><span class="p">,</span>
                    <span class="n">c</span><span class="o">.</span><span class="n">right</span><span class="p">,</span> <span class="n">c</span><span class="o">.</span><span class="n">multiplier</span><span class="p">,</span> <span class="n">c</span><span class="o">.</span><span class="n">exchange</span><span class="p">,</span>
                    <span class="n">c</span><span class="o">.</span><span class="n">primaryExchange</span><span class="p">,</span> <span class="n">c</span><span class="o">.</span><span class="n">currency</span><span class="p">,</span>
                    <span class="n">c</span><span class="o">.</span><span class="n">localSymbol</span><span class="p">,</span> <span class="n">c</span><span class="o">.</span><span class="n">tradingClass</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">s</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">field</span><span class="p">)</span>
            <span class="n">msg</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
            <span class="n">msg</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\0</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sendMsg</span><span class="p">(</span><span class="n">msg</span><span class="o">.</span><span class="n">getvalue</span><span class="p">())</span></div>

<div class="viewcode-block" id="Client.sendMsg"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.sendMsg">[docs]</a>    <span class="k">def</span> <span class="nf">sendMsg</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
        <span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
        <span class="n">t</span> <span class="o">=</span> <span class="n">loop</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
        <span class="n">times</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_timeQ</span>
        <span class="n">msgs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_msgQ</span>
        <span class="k">while</span> <span class="n">times</span> <span class="ow">and</span> <span class="n">t</span> <span class="o">-</span> <span class="n">times</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">RequestsInterval</span><span class="p">:</span>
            <span class="n">times</span><span class="o">.</span><span class="n">popleft</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">msg</span><span class="p">:</span>
            <span class="n">msgs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
        <span class="k">while</span> <span class="n">msgs</span> <span class="ow">and</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">times</span><span class="p">)</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">MaxRequests</span> <span class="ow">or</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">MaxRequests</span><span class="p">):</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="n">msgs</span><span class="o">.</span><span class="n">popleft</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">conn</span><span class="o">.</span><span class="n">sendMsg</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_prefix</span><span class="p">(</span><span class="n">msg</span><span class="o">.</span><span class="n">encode</span><span class="p">()))</span>
            <span class="n">times</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">isEnabledFor</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;&gt;&gt;&gt; </span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">msg</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\0</span><span class="s1">&#39;</span><span class="p">,</span> <span class="s1">&#39;,&#39;</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">msgs</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_isThrottling</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_isThrottling</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">throttleStart</span><span class="o">.</span><span class="n">emit</span><span class="p">()</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Started to throttle requests&#39;</span><span class="p">)</span>
            <span class="n">loop</span><span class="o">.</span><span class="n">call_at</span><span class="p">(</span>
                <span class="n">times</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">RequestsInterval</span><span class="p">,</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">sendMsg</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_isThrottling</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_isThrottling</span> <span class="o">=</span> <span class="kc">False</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">throttleEnd</span><span class="o">.</span><span class="n">emit</span><span class="p">()</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Stopped to throttle requests&#39;</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">_prefix</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
        <span class="c1"># prefix a message with its length</span>
        <span class="k">return</span> <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&gt;I&#39;</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">msg</span><span class="p">))</span> <span class="o">+</span> <span class="n">msg</span>

    <span class="k">def</span> <span class="nf">_onSocketHasData</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="n">debug</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">isEnabledFor</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_tcpDataArrived</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_tcpDataArrived</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_data</span> <span class="o">+=</span> <span class="n">data</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_numBytesRecv</span> <span class="o">+=</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>

        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="mi">4</span><span class="p">:</span>
                <span class="k">break</span>
            <span class="c1"># 4 byte prefix tells the message length</span>
            <span class="n">msgEnd</span> <span class="o">=</span> <span class="mi">4</span> <span class="o">+</span> <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s1">&#39;&gt;I&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[:</span><span class="mi">4</span><span class="p">])[</span><span class="mi">0</span><span class="p">]</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">msgEnd</span><span class="p">:</span>
                <span class="c1"># insufficient data for now</span>
                <span class="k">break</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="mi">4</span><span class="p">:</span><span class="n">msgEnd</span><span class="p">]</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="n">errors</span><span class="o">=</span><span class="s1">&#39;backslashreplace&#39;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="n">msgEnd</span><span class="p">:]</span>
            <span class="n">fields</span> <span class="o">=</span> <span class="n">msg</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\0</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="n">fields</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>  <span class="c1"># pop off last empty element</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_numMsgRecv</span> <span class="o">+=</span> <span class="mi">1</span>

            <span class="k">if</span> <span class="n">debug</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;&lt;&lt;&lt; </span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">,</span> <span class="s1">&#39;,&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">fields</span><span class="p">))</span>

            <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_serverVersion</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">fields</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
                <span class="c1"># this concludes the handshake</span>
                <span class="n">version</span><span class="p">,</span> <span class="n">_connTime</span> <span class="o">=</span> <span class="n">fields</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_serverVersion</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">version</span><span class="p">)</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_serverVersion</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">MinClientVersion</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_onSocketDisconnected</span><span class="p">(</span>
                        <span class="s1">&#39;TWS/gateway version must be &gt;= 972&#39;</span><span class="p">)</span>
                    <span class="k">return</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">decoder</span><span class="o">.</span><span class="n">serverVersion</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_serverVersion</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">connState</span> <span class="o">=</span> <span class="n">Client</span><span class="o">.</span><span class="n">CONNECTED</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">startApi</span><span class="p">()</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">connectAck</span><span class="p">()</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
                    <span class="sa">f</span><span class="s1">&#39;Logged on to server version </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_serverVersion</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_apiReady</span><span class="p">:</span>
                    <span class="c1"># snoop for nextValidId and managedAccounts response,</span>
                    <span class="c1"># when both are in then the client is ready</span>
                    <span class="n">msgId</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">fields</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
                    <span class="k">if</span> <span class="n">msgId</span> <span class="o">==</span> <span class="mi">9</span><span class="p">:</span>
                        <span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">validId</span> <span class="o">=</span> <span class="n">fields</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">updateReqId</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">validId</span><span class="p">))</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">_hasReqId</span> <span class="o">=</span> <span class="kc">True</span>
                    <span class="k">elif</span> <span class="n">msgId</span> <span class="o">==</span> <span class="mi">15</span><span class="p">:</span>
                        <span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">accts</span> <span class="o">=</span> <span class="n">fields</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">_accounts</span> <span class="o">=</span> <span class="p">[</span><span class="n">a</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">accts</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">)</span> <span class="k">if</span> <span class="n">a</span><span class="p">]</span>
                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_hasReqId</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_accounts</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">_apiReady</span> <span class="o">=</span> <span class="kc">True</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">apiStart</span><span class="o">.</span><span class="n">emit</span><span class="p">()</span>

                <span class="c1"># decode and handle the message</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">decoder</span><span class="o">.</span><span class="n">interpret</span><span class="p">(</span><span class="n">fields</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_tcpDataProcessed</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_tcpDataProcessed</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_onSocketDisconnected</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
        <span class="n">wasReady</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">isReady</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">isConnected</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Disconnected.&#39;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="ow">not</span> <span class="n">msg</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="s1">&#39;Peer closed connection.&#39;</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">wasReady</span><span class="p">:</span>
                <span class="n">msg</span> <span class="o">+=</span> <span class="sa">f</span><span class="s1">&#39; clientId </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">clientId</span><span class="si">}</span><span class="s1"> already in use?&#39;</span>
        <span class="k">if</span> <span class="n">msg</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">apiError</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">setEventsDone</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">wasReady</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">connectionClosed</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">wasReady</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">apiEnd</span><span class="o">.</span><span class="n">emit</span><span class="p">()</span>

    <span class="c1"># client request methods</span>
    <span class="c1"># the message type id is sent first, often followed by a version number</span>

<div class="viewcode-block" id="Client.reqMktData"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqMktData">[docs]</a>    <span class="k">def</span> <span class="nf">reqMktData</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">genericTickList</span><span class="p">,</span> <span class="n">snapshot</span><span class="p">,</span>
            <span class="n">regulatorySnapshot</span><span class="p">,</span> <span class="n">mktDataOptions</span><span class="p">):</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">contract</span><span class="o">.</span><span class="n">secType</span> <span class="o">==</span> <span class="s1">&#39;BAG&#39;</span><span class="p">:</span>
            <span class="n">legs</span> <span class="o">=</span> <span class="n">contract</span><span class="o">.</span><span class="n">comboLegs</span> <span class="ow">or</span> <span class="p">[]</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">legs</span><span class="p">)]</span>
            <span class="k">for</span> <span class="n">leg</span> <span class="ow">in</span> <span class="n">legs</span><span class="p">:</span>
                <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="n">leg</span><span class="o">.</span><span class="n">conId</span><span class="p">,</span> <span class="n">leg</span><span class="o">.</span><span class="n">ratio</span><span class="p">,</span> <span class="n">leg</span><span class="o">.</span><span class="n">action</span><span class="p">,</span> <span class="n">leg</span><span class="o">.</span><span class="n">exchange</span><span class="p">]</span>

        <span class="n">dnc</span> <span class="o">=</span> <span class="n">contract</span><span class="o">.</span><span class="n">deltaNeutralContract</span>
        <span class="k">if</span> <span class="n">dnc</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="kc">True</span><span class="p">,</span> <span class="n">dnc</span><span class="o">.</span><span class="n">conId</span><span class="p">,</span> <span class="n">dnc</span><span class="o">.</span><span class="n">delta</span><span class="p">,</span> <span class="n">dnc</span><span class="o">.</span><span class="n">price</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="kc">False</span><span class="p">]</span>

        <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span>
            <span class="n">genericTickList</span><span class="p">,</span> <span class="n">snapshot</span><span class="p">,</span> <span class="n">regulatorySnapshot</span><span class="p">,</span> <span class="n">mktDataOptions</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="o">*</span><span class="n">fields</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.cancelMktData"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.cancelMktData">[docs]</a>    <span class="k">def</span> <span class="nf">cancelMktData</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">reqId</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.placeOrder"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.placeOrder">[docs]</a>    <span class="k">def</span> <span class="nf">placeOrder</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">orderId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">order</span><span class="p">):</span>
        <span class="n">version</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">serverVersion</span><span class="p">()</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="p">[</span><span class="mi">3</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">version</span> <span class="o">&lt;</span> <span class="mi">145</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="mi">45</span><span class="p">]</span>
        <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span>
            <span class="n">orderId</span><span class="p">,</span>
            <span class="n">contract</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">secIdType</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">secId</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">action</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">totalQuantity</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">orderType</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">lmtPrice</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">auxPrice</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">tif</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">ocaGroup</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">account</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">openClose</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">origin</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">orderRef</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">transmit</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">parentId</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">blockOrder</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">sweepToFill</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">displaySize</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">triggerMethod</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">outsideRth</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">hidden</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">contract</span><span class="o">.</span><span class="n">secType</span> <span class="o">==</span> <span class="s1">&#39;BAG&#39;</span><span class="p">:</span>
            <span class="n">legs</span> <span class="o">=</span> <span class="n">contract</span><span class="o">.</span><span class="n">comboLegs</span> <span class="ow">or</span> <span class="p">[]</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">legs</span><span class="p">)]</span>
            <span class="k">for</span> <span class="n">leg</span> <span class="ow">in</span> <span class="n">legs</span><span class="p">:</span>
                <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span>
                    <span class="n">leg</span><span class="o">.</span><span class="n">conId</span><span class="p">,</span>
                    <span class="n">leg</span><span class="o">.</span><span class="n">ratio</span><span class="p">,</span>
                    <span class="n">leg</span><span class="o">.</span><span class="n">action</span><span class="p">,</span>
                    <span class="n">leg</span><span class="o">.</span><span class="n">exchange</span><span class="p">,</span>
                    <span class="n">leg</span><span class="o">.</span><span class="n">openClose</span><span class="p">,</span>
                    <span class="n">leg</span><span class="o">.</span><span class="n">shortSaleSlot</span><span class="p">,</span>
                    <span class="n">leg</span><span class="o">.</span><span class="n">designatedLocation</span><span class="p">,</span>
                    <span class="n">leg</span><span class="o">.</span><span class="n">exemptCode</span><span class="p">]</span>

            <span class="n">legs</span> <span class="o">=</span> <span class="n">order</span><span class="o">.</span><span class="n">orderComboLegs</span> <span class="ow">or</span> <span class="p">[]</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">legs</span><span class="p">)]</span>
            <span class="k">for</span> <span class="n">leg</span> <span class="ow">in</span> <span class="n">legs</span><span class="p">:</span>
                <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="n">leg</span><span class="o">.</span><span class="n">price</span><span class="p">]</span>

            <span class="n">params</span> <span class="o">=</span> <span class="n">order</span><span class="o">.</span><span class="n">smartComboRoutingParams</span> <span class="ow">or</span> <span class="p">[]</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">params</span><span class="p">)]</span>
            <span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">params</span><span class="p">:</span>
                <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="n">param</span><span class="o">.</span><span class="n">tag</span><span class="p">,</span> <span class="n">param</span><span class="o">.</span><span class="n">value</span><span class="p">]</span>

        <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span>
            <span class="s1">&#39;&#39;</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">discretionaryAmt</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">goodAfterTime</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">goodTillDate</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">faGroup</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">faMethod</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">faPercentage</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">faProfile</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">modelCode</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">shortSaleSlot</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">designatedLocation</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">exemptCode</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">ocaType</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">rule80A</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">settlingFirm</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">allOrNone</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">minQty</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">percentOffset</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">eTradeOnly</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">firmQuoteOnly</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">nbboPriceCap</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">auctionStrategy</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">startingPrice</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">stockRefPrice</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">delta</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">stockRangeLower</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">stockRangeUpper</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">overridePercentageConstraints</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">volatility</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">volatilityType</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">deltaNeutralOrderType</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">deltaNeutralAuxPrice</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">order</span><span class="o">.</span><span class="n">deltaNeutralOrderType</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span>
                <span class="n">order</span><span class="o">.</span><span class="n">deltaNeutralConId</span><span class="p">,</span>
                <span class="n">order</span><span class="o">.</span><span class="n">deltaNeutralSettlingFirm</span><span class="p">,</span>
                <span class="n">order</span><span class="o">.</span><span class="n">deltaNeutralClearingAccount</span><span class="p">,</span>
                <span class="n">order</span><span class="o">.</span><span class="n">deltaNeutralClearingIntent</span><span class="p">,</span>
                <span class="n">order</span><span class="o">.</span><span class="n">deltaNeutralOpenClose</span><span class="p">,</span>
                <span class="n">order</span><span class="o">.</span><span class="n">deltaNeutralShortSale</span><span class="p">,</span>
                <span class="n">order</span><span class="o">.</span><span class="n">deltaNeutralShortSaleSlot</span><span class="p">,</span>
                <span class="n">order</span><span class="o">.</span><span class="n">deltaNeutralDesignatedLocation</span><span class="p">]</span>

        <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span>
            <span class="n">order</span><span class="o">.</span><span class="n">continuousUpdate</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">referencePriceType</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">trailStopPrice</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">trailingPercent</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">scaleInitLevelSize</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">scaleSubsLevelSize</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">scalePriceIncrement</span><span class="p">]</span>

        <span class="k">if</span> <span class="p">(</span><span class="mi">0</span> <span class="o">&lt;</span> <span class="n">order</span><span class="o">.</span><span class="n">scalePriceIncrement</span> <span class="o">&lt;</span> <span class="n">UNSET_DOUBLE</span><span class="p">):</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span>
                <span class="n">order</span><span class="o">.</span><span class="n">scalePriceAdjustValue</span><span class="p">,</span>
                <span class="n">order</span><span class="o">.</span><span class="n">scalePriceAdjustInterval</span><span class="p">,</span>
                <span class="n">order</span><span class="o">.</span><span class="n">scaleProfitOffset</span><span class="p">,</span>
                <span class="n">order</span><span class="o">.</span><span class="n">scaleAutoReset</span><span class="p">,</span>
                <span class="n">order</span><span class="o">.</span><span class="n">scaleInitPosition</span><span class="p">,</span>
                <span class="n">order</span><span class="o">.</span><span class="n">scaleInitFillQty</span><span class="p">,</span>
                <span class="n">order</span><span class="o">.</span><span class="n">scaleRandomPercent</span><span class="p">]</span>

        <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span>
            <span class="n">order</span><span class="o">.</span><span class="n">scaleTable</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">activeStartTime</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">activeStopTime</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">hedgeType</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">order</span><span class="o">.</span><span class="n">hedgeType</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="n">order</span><span class="o">.</span><span class="n">hedgeParam</span><span class="p">]</span>

        <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span>
            <span class="n">order</span><span class="o">.</span><span class="n">optOutSmartRouting</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">clearingAccount</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">clearingIntent</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">notHeld</span><span class="p">]</span>

        <span class="n">dnc</span> <span class="o">=</span> <span class="n">contract</span><span class="o">.</span><span class="n">deltaNeutralContract</span>
        <span class="k">if</span> <span class="n">dnc</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="kc">True</span><span class="p">,</span> <span class="n">dnc</span><span class="o">.</span><span class="n">conId</span><span class="p">,</span> <span class="n">dnc</span><span class="o">.</span><span class="n">delta</span><span class="p">,</span> <span class="n">dnc</span><span class="o">.</span><span class="n">price</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="kc">False</span><span class="p">]</span>

        <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="n">order</span><span class="o">.</span><span class="n">algoStrategy</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">order</span><span class="o">.</span><span class="n">algoStrategy</span><span class="p">:</span>
            <span class="n">params</span> <span class="o">=</span> <span class="n">order</span><span class="o">.</span><span class="n">algoParams</span> <span class="ow">or</span> <span class="p">[]</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">params</span><span class="p">)]</span>
            <span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">params</span><span class="p">:</span>
                <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="n">param</span><span class="o">.</span><span class="n">tag</span><span class="p">,</span> <span class="n">param</span><span class="o">.</span><span class="n">value</span><span class="p">]</span>

        <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span>
            <span class="n">order</span><span class="o">.</span><span class="n">algoId</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">whatIf</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">orderMiscOptions</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">solicited</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">randomizeSize</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">randomizePrice</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">order</span><span class="o">.</span><span class="n">orderType</span> <span class="o">==</span> <span class="s1">&#39;PEG BENCH&#39;</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span>
                <span class="n">order</span><span class="o">.</span><span class="n">referenceContractId</span><span class="p">,</span>
                <span class="n">order</span><span class="o">.</span><span class="n">isPeggedChangeAmountDecrease</span><span class="p">,</span>
                <span class="n">order</span><span class="o">.</span><span class="n">peggedChangeAmount</span><span class="p">,</span>
                <span class="n">order</span><span class="o">.</span><span class="n">referenceChangeAmount</span><span class="p">,</span>
                <span class="n">order</span><span class="o">.</span><span class="n">referenceExchangeId</span><span class="p">]</span>

        <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">order</span><span class="o">.</span><span class="n">conditions</span><span class="p">)]</span>
        <span class="k">if</span> <span class="n">order</span><span class="o">.</span><span class="n">conditions</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">cond</span> <span class="ow">in</span> <span class="n">order</span><span class="o">.</span><span class="n">conditions</span><span class="p">:</span>
                <span class="n">fields</span> <span class="o">+=</span> <span class="n">dataclassAsTuple</span><span class="p">(</span><span class="n">cond</span><span class="p">)</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span>
                <span class="n">order</span><span class="o">.</span><span class="n">conditionsIgnoreRth</span><span class="p">,</span>
                <span class="n">order</span><span class="o">.</span><span class="n">conditionsCancelOrder</span><span class="p">]</span>

        <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span>
            <span class="n">order</span><span class="o">.</span><span class="n">adjustedOrderType</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">triggerPrice</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">lmtPriceOffset</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">adjustedStopPrice</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">adjustedStopLimitPrice</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">adjustedTrailingAmount</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">adjustableTrailingUnit</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">extOperator</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">softDollarTier</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">softDollarTier</span><span class="o">.</span><span class="n">val</span><span class="p">,</span>
            <span class="n">order</span><span class="o">.</span><span class="n">cashQty</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">version</span> <span class="o">&gt;=</span> <span class="mi">138</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="n">order</span><span class="o">.</span><span class="n">mifid2DecisionMaker</span><span class="p">,</span> <span class="n">order</span><span class="o">.</span><span class="n">mifid2DecisionAlgo</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">version</span> <span class="o">&gt;=</span> <span class="mi">139</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="n">order</span><span class="o">.</span><span class="n">mifid2ExecutionTrader</span><span class="p">,</span> <span class="n">order</span><span class="o">.</span><span class="n">mifid2ExecutionAlgo</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">version</span> <span class="o">&gt;=</span> <span class="mi">141</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="n">order</span><span class="o">.</span><span class="n">dontUseAutoPriceForHedge</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">version</span> <span class="o">&gt;=</span> <span class="mi">145</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="n">order</span><span class="o">.</span><span class="n">isOmsContainer</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">version</span> <span class="o">&gt;=</span> <span class="mi">148</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="n">order</span><span class="o">.</span><span class="n">discretionaryUpToLimitPrice</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">version</span> <span class="o">&gt;=</span> <span class="mi">151</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="n">order</span><span class="o">.</span><span class="n">usePriceMgmtAlgo</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">version</span> <span class="o">&gt;=</span> <span class="mi">158</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="n">order</span><span class="o">.</span><span class="n">duration</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">version</span> <span class="o">&gt;=</span> <span class="mi">160</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="n">order</span><span class="o">.</span><span class="n">postToAts</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">version</span> <span class="o">&gt;=</span> <span class="mi">162</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="n">order</span><span class="o">.</span><span class="n">autoCancelParent</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="o">*</span><span class="n">fields</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.cancelOrder"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.cancelOrder">[docs]</a>    <span class="k">def</span> <span class="nf">cancelOrder</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">orderId</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">orderId</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqOpenOrders"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqOpenOrders">[docs]</a>    <span class="k">def</span> <span class="nf">reqOpenOrders</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqAccountUpdates"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqAccountUpdates">[docs]</a>    <span class="k">def</span> <span class="nf">reqAccountUpdates</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">subscribe</span><span class="p">,</span> <span class="n">acctCode</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">subscribe</span><span class="p">,</span> <span class="n">acctCode</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqExecutions"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqExecutions">[docs]</a>    <span class="k">def</span> <span class="nf">reqExecutions</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">execFilter</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span>
            <span class="mi">7</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span>
            <span class="n">execFilter</span><span class="o">.</span><span class="n">clientId</span><span class="p">,</span>
            <span class="n">execFilter</span><span class="o">.</span><span class="n">acctCode</span><span class="p">,</span>
            <span class="n">execFilter</span><span class="o">.</span><span class="n">time</span><span class="p">,</span>
            <span class="n">execFilter</span><span class="o">.</span><span class="n">symbol</span><span class="p">,</span>
            <span class="n">execFilter</span><span class="o">.</span><span class="n">secType</span><span class="p">,</span>
            <span class="n">execFilter</span><span class="o">.</span><span class="n">exchange</span><span class="p">,</span>
            <span class="n">execFilter</span><span class="o">.</span><span class="n">side</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqIds"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqIds">[docs]</a>    <span class="k">def</span> <span class="nf">reqIds</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">numIds</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">8</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">numIds</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqContractDetails"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqContractDetails">[docs]</a>    <span class="k">def</span> <span class="nf">reqContractDetails</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span>
            <span class="mi">9</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">contract</span><span class="o">.</span><span class="n">includeExpired</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">secIdType</span><span class="p">,</span> <span class="n">contract</span><span class="o">.</span><span class="n">secId</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqMktDepth"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqMktDepth">[docs]</a>    <span class="k">def</span> <span class="nf">reqMktDepth</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">numRows</span><span class="p">,</span> <span class="n">isSmartDepth</span><span class="p">,</span> <span class="n">mktDepthOptions</span><span class="p">):</span>
        <span class="n">version</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">serverVersion</span><span class="p">()</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="p">[</span>
            <span class="mi">10</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">conId</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">symbol</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">secType</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">lastTradeDateOrContractMonth</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">strike</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">right</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">multiplier</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">exchange</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">version</span> <span class="o">&gt;=</span> <span class="mi">149</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="n">contract</span><span class="o">.</span><span class="n">primaryExchange</span><span class="p">]</span>
        <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">currency</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">localSymbol</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">tradingClass</span><span class="p">,</span>
            <span class="n">numRows</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">version</span> <span class="o">&gt;=</span> <span class="mi">146</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="n">isSmartDepth</span><span class="p">]</span>
        <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="n">mktDepthOptions</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="o">*</span><span class="n">fields</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.cancelMktDepth"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.cancelMktDepth">[docs]</a>    <span class="k">def</span> <span class="nf">cancelMktDepth</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">isSmartDepth</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">11</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">isSmartDepth</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqNewsBulletins"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqNewsBulletins">[docs]</a>    <span class="k">def</span> <span class="nf">reqNewsBulletins</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">allMsgs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">12</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">allMsgs</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.cancelNewsBulletins"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.cancelNewsBulletins">[docs]</a>    <span class="k">def</span> <span class="nf">cancelNewsBulletins</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">13</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.setServerLogLevel"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.setServerLogLevel">[docs]</a>    <span class="k">def</span> <span class="nf">setServerLogLevel</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">logLevel</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">14</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">logLevel</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqAutoOpenOrders"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqAutoOpenOrders">[docs]</a>    <span class="k">def</span> <span class="nf">reqAutoOpenOrders</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">bAutoBind</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">15</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">bAutoBind</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqAllOpenOrders"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqAllOpenOrders">[docs]</a>    <span class="k">def</span> <span class="nf">reqAllOpenOrders</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">16</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqManagedAccts"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqManagedAccts">[docs]</a>    <span class="k">def</span> <span class="nf">reqManagedAccts</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">17</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.requestFA"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.requestFA">[docs]</a>    <span class="k">def</span> <span class="nf">requestFA</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">faData</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">18</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">faData</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.replaceFA"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.replaceFA">[docs]</a>    <span class="k">def</span> <span class="nf">replaceFA</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">faData</span><span class="p">,</span> <span class="n">cxml</span><span class="p">):</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="p">[</span><span class="mi">19</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">faData</span><span class="p">,</span> <span class="n">cxml</span><span class="p">]</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">serverVersion</span><span class="p">()</span> <span class="o">&gt;=</span> <span class="mi">157</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="n">reqId</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="o">*</span><span class="n">fields</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqHistoricalData"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqHistoricalData">[docs]</a>    <span class="k">def</span> <span class="nf">reqHistoricalData</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">endDateTime</span><span class="p">,</span> <span class="n">durationStr</span><span class="p">,</span> <span class="n">barSizeSetting</span><span class="p">,</span>
            <span class="n">whatToShow</span><span class="p">,</span> <span class="n">useRTH</span><span class="p">,</span> <span class="n">formatDate</span><span class="p">,</span> <span class="n">keepUpToDate</span><span class="p">,</span> <span class="n">chartOptions</span><span class="p">):</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="p">[</span>
            <span class="mi">20</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">contract</span><span class="o">.</span><span class="n">includeExpired</span><span class="p">,</span>
            <span class="n">endDateTime</span><span class="p">,</span> <span class="n">barSizeSetting</span><span class="p">,</span> <span class="n">durationStr</span><span class="p">,</span> <span class="n">useRTH</span><span class="p">,</span>
            <span class="n">whatToShow</span><span class="p">,</span> <span class="n">formatDate</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">contract</span><span class="o">.</span><span class="n">secType</span> <span class="o">==</span> <span class="s1">&#39;BAG&#39;</span><span class="p">:</span>
            <span class="n">legs</span> <span class="o">=</span> <span class="n">contract</span><span class="o">.</span><span class="n">comboLegs</span> <span class="ow">or</span> <span class="p">[]</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">legs</span><span class="p">)]</span>
            <span class="k">for</span> <span class="n">leg</span> <span class="ow">in</span> <span class="n">legs</span><span class="p">:</span>
                <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="n">leg</span><span class="o">.</span><span class="n">conId</span><span class="p">,</span> <span class="n">leg</span><span class="o">.</span><span class="n">ratio</span><span class="p">,</span> <span class="n">leg</span><span class="o">.</span><span class="n">action</span><span class="p">,</span> <span class="n">leg</span><span class="o">.</span><span class="n">exchange</span><span class="p">]</span>

        <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="n">keepUpToDate</span><span class="p">,</span> <span class="n">chartOptions</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="o">*</span><span class="n">fields</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.exerciseOptions"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.exerciseOptions">[docs]</a>    <span class="k">def</span> <span class="nf">exerciseOptions</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">exerciseAction</span><span class="p">,</span>
            <span class="n">exerciseQuantity</span><span class="p">,</span> <span class="n">account</span><span class="p">,</span> <span class="n">override</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span>
            <span class="mi">21</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">conId</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">symbol</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">secType</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">lastTradeDateOrContractMonth</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">strike</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">right</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">multiplier</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">exchange</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">currency</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">localSymbol</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">tradingClass</span><span class="p">,</span>
            <span class="n">exerciseAction</span><span class="p">,</span> <span class="n">exerciseQuantity</span><span class="p">,</span> <span class="n">account</span><span class="p">,</span> <span class="n">override</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqScannerSubscription"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqScannerSubscription">[docs]</a>    <span class="k">def</span> <span class="nf">reqScannerSubscription</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">subscription</span><span class="p">,</span> <span class="n">scannerSubscriptionOptions</span><span class="p">,</span>
            <span class="n">scannerSubscriptionFilterOptions</span><span class="p">):</span>
        <span class="n">version</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">serverVersion</span><span class="p">()</span>
        <span class="n">sub</span> <span class="o">=</span> <span class="n">subscription</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="p">[</span><span class="mi">22</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">version</span> <span class="o">&lt;</span> <span class="mi">143</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="mi">4</span><span class="p">]</span>
        <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span>
            <span class="n">reqId</span><span class="p">,</span>
            <span class="n">sub</span><span class="o">.</span><span class="n">numberOfRows</span><span class="p">,</span>
            <span class="n">sub</span><span class="o">.</span><span class="n">instrument</span><span class="p">,</span>
            <span class="n">sub</span><span class="o">.</span><span class="n">locationCode</span><span class="p">,</span>
            <span class="n">sub</span><span class="o">.</span><span class="n">scanCode</span><span class="p">,</span>
            <span class="n">sub</span><span class="o">.</span><span class="n">abovePrice</span><span class="p">,</span>
            <span class="n">sub</span><span class="o">.</span><span class="n">belowPrice</span><span class="p">,</span>
            <span class="n">sub</span><span class="o">.</span><span class="n">aboveVolume</span><span class="p">,</span>
            <span class="n">sub</span><span class="o">.</span><span class="n">marketCapAbove</span><span class="p">,</span>
            <span class="n">sub</span><span class="o">.</span><span class="n">marketCapBelow</span><span class="p">,</span>
            <span class="n">sub</span><span class="o">.</span><span class="n">moodyRatingAbove</span><span class="p">,</span>
            <span class="n">sub</span><span class="o">.</span><span class="n">moodyRatingBelow</span><span class="p">,</span>
            <span class="n">sub</span><span class="o">.</span><span class="n">spRatingAbove</span><span class="p">,</span>
            <span class="n">sub</span><span class="o">.</span><span class="n">spRatingBelow</span><span class="p">,</span>
            <span class="n">sub</span><span class="o">.</span><span class="n">maturityDateAbove</span><span class="p">,</span>
            <span class="n">sub</span><span class="o">.</span><span class="n">maturityDateBelow</span><span class="p">,</span>
            <span class="n">sub</span><span class="o">.</span><span class="n">couponRateAbove</span><span class="p">,</span>
            <span class="n">sub</span><span class="o">.</span><span class="n">couponRateBelow</span><span class="p">,</span>
            <span class="n">sub</span><span class="o">.</span><span class="n">excludeConvertible</span><span class="p">,</span>
            <span class="n">sub</span><span class="o">.</span><span class="n">averageOptionVolumeAbove</span><span class="p">,</span>
            <span class="n">sub</span><span class="o">.</span><span class="n">scannerSettingPairs</span><span class="p">,</span>
            <span class="n">sub</span><span class="o">.</span><span class="n">stockTypeFilter</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">version</span> <span class="o">&gt;=</span> <span class="mi">143</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="n">scannerSubscriptionFilterOptions</span><span class="p">]</span>
        <span class="n">fields</span> <span class="o">+=</span> <span class="p">[</span><span class="n">scannerSubscriptionOptions</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="o">*</span><span class="n">fields</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.cancelScannerSubscription"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.cancelScannerSubscription">[docs]</a>    <span class="k">def</span> <span class="nf">cancelScannerSubscription</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">23</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">reqId</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqScannerParameters"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqScannerParameters">[docs]</a>    <span class="k">def</span> <span class="nf">reqScannerParameters</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">24</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.cancelHistoricalData"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.cancelHistoricalData">[docs]</a>    <span class="k">def</span> <span class="nf">cancelHistoricalData</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">25</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">reqId</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqCurrentTime"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqCurrentTime">[docs]</a>    <span class="k">def</span> <span class="nf">reqCurrentTime</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">49</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqRealTimeBars"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqRealTimeBars">[docs]</a>    <span class="k">def</span> <span class="nf">reqRealTimeBars</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">barSize</span><span class="p">,</span> <span class="n">whatToShow</span><span class="p">,</span>
            <span class="n">useRTH</span><span class="p">,</span> <span class="n">realTimeBarsOptions</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span>
            <span class="mi">50</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">barSize</span><span class="p">,</span> <span class="n">whatToShow</span><span class="p">,</span>
            <span class="n">useRTH</span><span class="p">,</span> <span class="n">realTimeBarsOptions</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.cancelRealTimeBars"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.cancelRealTimeBars">[docs]</a>    <span class="k">def</span> <span class="nf">cancelRealTimeBars</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">51</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">reqId</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqFundamentalData"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqFundamentalData">[docs]</a>    <span class="k">def</span> <span class="nf">reqFundamentalData</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">reportType</span><span class="p">,</span> <span class="n">fundamentalDataOptions</span><span class="p">):</span>
        <span class="n">options</span> <span class="o">=</span> <span class="n">fundamentalDataOptions</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span>
            <span class="mi">52</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">conId</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">symbol</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">secType</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">exchange</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">primaryExchange</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">currency</span><span class="p">,</span>
            <span class="n">contract</span><span class="o">.</span><span class="n">localSymbol</span><span class="p">,</span>
            <span class="n">reportType</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">options</span><span class="p">),</span> <span class="n">options</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.cancelFundamentalData"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.cancelFundamentalData">[docs]</a>    <span class="k">def</span> <span class="nf">cancelFundamentalData</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">53</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">reqId</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.calculateImpliedVolatility"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.calculateImpliedVolatility">[docs]</a>    <span class="k">def</span> <span class="nf">calculateImpliedVolatility</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">optionPrice</span><span class="p">,</span> <span class="n">underPrice</span><span class="p">,</span> <span class="n">implVolOptions</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span>
            <span class="mi">54</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">optionPrice</span><span class="p">,</span> <span class="n">underPrice</span><span class="p">,</span>
            <span class="nb">len</span><span class="p">(</span><span class="n">implVolOptions</span><span class="p">),</span> <span class="n">implVolOptions</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.calculateOptionPrice"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.calculateOptionPrice">[docs]</a>    <span class="k">def</span> <span class="nf">calculateOptionPrice</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">volatility</span><span class="p">,</span> <span class="n">underPrice</span><span class="p">,</span> <span class="n">optPrcOptions</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span>
            <span class="mi">55</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">volatility</span><span class="p">,</span> <span class="n">underPrice</span><span class="p">,</span>
            <span class="nb">len</span><span class="p">(</span><span class="n">optPrcOptions</span><span class="p">),</span> <span class="n">optPrcOptions</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.cancelCalculateImpliedVolatility"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.cancelCalculateImpliedVolatility">[docs]</a>    <span class="k">def</span> <span class="nf">cancelCalculateImpliedVolatility</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">56</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">reqId</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.cancelCalculateOptionPrice"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.cancelCalculateOptionPrice">[docs]</a>    <span class="k">def</span> <span class="nf">cancelCalculateOptionPrice</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">57</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">reqId</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqGlobalCancel"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqGlobalCancel">[docs]</a>    <span class="k">def</span> <span class="nf">reqGlobalCancel</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">58</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqMarketDataType"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqMarketDataType">[docs]</a>    <span class="k">def</span> <span class="nf">reqMarketDataType</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">marketDataType</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">59</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">marketDataType</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqPositions"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqPositions">[docs]</a>    <span class="k">def</span> <span class="nf">reqPositions</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">61</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqAccountSummary"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqAccountSummary">[docs]</a>    <span class="k">def</span> <span class="nf">reqAccountSummary</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">groupName</span><span class="p">,</span> <span class="n">tags</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">62</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">groupName</span><span class="p">,</span> <span class="n">tags</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.cancelAccountSummary"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.cancelAccountSummary">[docs]</a>    <span class="k">def</span> <span class="nf">cancelAccountSummary</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">63</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">reqId</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.cancelPositions"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.cancelPositions">[docs]</a>    <span class="k">def</span> <span class="nf">cancelPositions</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">64</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.verifyRequest"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.verifyRequest">[docs]</a>    <span class="k">def</span> <span class="nf">verifyRequest</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">apiName</span><span class="p">,</span> <span class="n">apiVersion</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">65</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">apiName</span><span class="p">,</span> <span class="n">apiVersion</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.verifyMessage"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.verifyMessage">[docs]</a>    <span class="k">def</span> <span class="nf">verifyMessage</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">apiData</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">66</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">apiData</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.queryDisplayGroups"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.queryDisplayGroups">[docs]</a>    <span class="k">def</span> <span class="nf">queryDisplayGroups</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">67</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">reqId</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.subscribeToGroupEvents"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.subscribeToGroupEvents">[docs]</a>    <span class="k">def</span> <span class="nf">subscribeToGroupEvents</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">groupId</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">68</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">groupId</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.updateDisplayGroup"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.updateDisplayGroup">[docs]</a>    <span class="k">def</span> <span class="nf">updateDisplayGroup</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">contractInfo</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">69</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">contractInfo</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.unsubscribeFromGroupEvents"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.unsubscribeFromGroupEvents">[docs]</a>    <span class="k">def</span> <span class="nf">unsubscribeFromGroupEvents</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">70</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">reqId</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.startApi"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.startApi">[docs]</a>    <span class="k">def</span> <span class="nf">startApi</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">71</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">clientId</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">optCapab</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.verifyAndAuthRequest"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.verifyAndAuthRequest">[docs]</a>    <span class="k">def</span> <span class="nf">verifyAndAuthRequest</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">apiName</span><span class="p">,</span> <span class="n">apiVersion</span><span class="p">,</span> <span class="n">opaqueIsvKey</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">72</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">apiName</span><span class="p">,</span> <span class="n">apiVersion</span><span class="p">,</span> <span class="n">opaqueIsvKey</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.verifyAndAuthMessage"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.verifyAndAuthMessage">[docs]</a>    <span class="k">def</span> <span class="nf">verifyAndAuthMessage</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">apiData</span><span class="p">,</span> <span class="n">xyzResponse</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">73</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">apiData</span><span class="p">,</span> <span class="n">xyzResponse</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqPositionsMulti"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqPositionsMulti">[docs]</a>    <span class="k">def</span> <span class="nf">reqPositionsMulti</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">account</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">74</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">account</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.cancelPositionsMulti"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.cancelPositionsMulti">[docs]</a>    <span class="k">def</span> <span class="nf">cancelPositionsMulti</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">75</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">reqId</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqAccountUpdatesMulti"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqAccountUpdatesMulti">[docs]</a>    <span class="k">def</span> <span class="nf">reqAccountUpdatesMulti</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">account</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">,</span> <span class="n">ledgerAndNLV</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">76</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">account</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">,</span> <span class="n">ledgerAndNLV</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.cancelAccountUpdatesMulti"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.cancelAccountUpdatesMulti">[docs]</a>    <span class="k">def</span> <span class="nf">cancelAccountUpdatesMulti</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">77</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">reqId</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqSecDefOptParams"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqSecDefOptParams">[docs]</a>    <span class="k">def</span> <span class="nf">reqSecDefOptParams</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">underlyingSymbol</span><span class="p">,</span> <span class="n">futFopExchange</span><span class="p">,</span>
            <span class="n">underlyingSecType</span><span class="p">,</span> <span class="n">underlyingConId</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span>
            <span class="mi">78</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">underlyingSymbol</span><span class="p">,</span> <span class="n">futFopExchange</span><span class="p">,</span>
            <span class="n">underlyingSecType</span><span class="p">,</span> <span class="n">underlyingConId</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqSoftDollarTiers"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqSoftDollarTiers">[docs]</a>    <span class="k">def</span> <span class="nf">reqSoftDollarTiers</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">79</span><span class="p">,</span> <span class="n">reqId</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqFamilyCodes"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqFamilyCodes">[docs]</a>    <span class="k">def</span> <span class="nf">reqFamilyCodes</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">80</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqMatchingSymbols"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqMatchingSymbols">[docs]</a>    <span class="k">def</span> <span class="nf">reqMatchingSymbols</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">pattern</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">81</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">pattern</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqMktDepthExchanges"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqMktDepthExchanges">[docs]</a>    <span class="k">def</span> <span class="nf">reqMktDepthExchanges</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">82</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqSmartComponents"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqSmartComponents">[docs]</a>    <span class="k">def</span> <span class="nf">reqSmartComponents</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">bboExchange</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">83</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">bboExchange</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqNewsArticle"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqNewsArticle">[docs]</a>    <span class="k">def</span> <span class="nf">reqNewsArticle</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">providerCode</span><span class="p">,</span> <span class="n">articleId</span><span class="p">,</span> <span class="n">newsArticleOptions</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">84</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">providerCode</span><span class="p">,</span> <span class="n">articleId</span><span class="p">,</span> <span class="n">newsArticleOptions</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqNewsProviders"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqNewsProviders">[docs]</a>    <span class="k">def</span> <span class="nf">reqNewsProviders</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">85</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqHistoricalNews"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqHistoricalNews">[docs]</a>    <span class="k">def</span> <span class="nf">reqHistoricalNews</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">conId</span><span class="p">,</span> <span class="n">providerCodes</span><span class="p">,</span> <span class="n">startDateTime</span><span class="p">,</span> <span class="n">endDateTime</span><span class="p">,</span>
            <span class="n">totalResults</span><span class="p">,</span> <span class="n">historicalNewsOptions</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span>
            <span class="mi">86</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">conId</span><span class="p">,</span> <span class="n">providerCodes</span><span class="p">,</span> <span class="n">startDateTime</span><span class="p">,</span> <span class="n">endDateTime</span><span class="p">,</span>
            <span class="n">totalResults</span><span class="p">,</span> <span class="n">historicalNewsOptions</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqHeadTimeStamp"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqHeadTimeStamp">[docs]</a>    <span class="k">def</span> <span class="nf">reqHeadTimeStamp</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">whatToShow</span><span class="p">,</span> <span class="n">useRTH</span><span class="p">,</span> <span class="n">formatDate</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span>
            <span class="mi">87</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">contract</span><span class="o">.</span><span class="n">includeExpired</span><span class="p">,</span>
            <span class="n">useRTH</span><span class="p">,</span> <span class="n">whatToShow</span><span class="p">,</span> <span class="n">formatDate</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqHistogramData"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqHistogramData">[docs]</a>    <span class="k">def</span> <span class="nf">reqHistogramData</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tickerId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">useRTH</span><span class="p">,</span> <span class="n">timePeriod</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span>
            <span class="mi">88</span><span class="p">,</span> <span class="n">tickerId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">contract</span><span class="o">.</span><span class="n">includeExpired</span><span class="p">,</span>
            <span class="n">useRTH</span><span class="p">,</span> <span class="n">timePeriod</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.cancelHistogramData"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.cancelHistogramData">[docs]</a>    <span class="k">def</span> <span class="nf">cancelHistogramData</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tickerId</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">89</span><span class="p">,</span> <span class="n">tickerId</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.cancelHeadTimeStamp"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.cancelHeadTimeStamp">[docs]</a>    <span class="k">def</span> <span class="nf">cancelHeadTimeStamp</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">90</span><span class="p">,</span> <span class="n">reqId</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqMarketRule"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqMarketRule">[docs]</a>    <span class="k">def</span> <span class="nf">reqMarketRule</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">marketRuleId</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">91</span><span class="p">,</span> <span class="n">marketRuleId</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqPnL"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqPnL">[docs]</a>    <span class="k">def</span> <span class="nf">reqPnL</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">account</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">92</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">account</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.cancelPnL"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.cancelPnL">[docs]</a>    <span class="k">def</span> <span class="nf">cancelPnL</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">93</span><span class="p">,</span> <span class="n">reqId</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqPnLSingle"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqPnLSingle">[docs]</a>    <span class="k">def</span> <span class="nf">reqPnLSingle</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">account</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">,</span> <span class="n">conid</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">94</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">account</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">,</span> <span class="n">conid</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.cancelPnLSingle"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.cancelPnLSingle">[docs]</a>    <span class="k">def</span> <span class="nf">cancelPnLSingle</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">95</span><span class="p">,</span> <span class="n">reqId</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqHistoricalTicks"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqHistoricalTicks">[docs]</a>    <span class="k">def</span> <span class="nf">reqHistoricalTicks</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">startDateTime</span><span class="p">,</span> <span class="n">endDateTime</span><span class="p">,</span>
            <span class="n">numberOfTicks</span><span class="p">,</span> <span class="n">whatToShow</span><span class="p">,</span> <span class="n">useRth</span><span class="p">,</span> <span class="n">ignoreSize</span><span class="p">,</span> <span class="n">miscOptions</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span>
            <span class="mi">96</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">contract</span><span class="o">.</span><span class="n">includeExpired</span><span class="p">,</span>
            <span class="n">startDateTime</span><span class="p">,</span> <span class="n">endDateTime</span><span class="p">,</span> <span class="n">numberOfTicks</span><span class="p">,</span> <span class="n">whatToShow</span><span class="p">,</span>
            <span class="n">useRth</span><span class="p">,</span> <span class="n">ignoreSize</span><span class="p">,</span> <span class="n">miscOptions</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqTickByTickData"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqTickByTickData">[docs]</a>    <span class="k">def</span> <span class="nf">reqTickByTickData</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">tickType</span><span class="p">,</span> <span class="n">numberOfTicks</span><span class="p">,</span> <span class="n">ignoreSize</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">97</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">tickType</span><span class="p">,</span> <span class="n">numberOfTicks</span><span class="p">,</span> <span class="n">ignoreSize</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.cancelTickByTickData"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.cancelTickByTickData">[docs]</a>    <span class="k">def</span> <span class="nf">cancelTickByTickData</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">98</span><span class="p">,</span> <span class="n">reqId</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.reqCompletedOrders"><a class="viewcode-back" href="../../api.html#ib_insync.client.Client.reqCompletedOrders">[docs]</a>    <span class="k">def</span> <span class="nf">reqCompletedOrders</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">apiOnly</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">99</span><span class="p">,</span> <span class="n">apiOnly</span><span class="p">)</span></div></div>
</pre></div>

           </div>
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2021, Ewald de Wit.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>